package org.jeecg.modules.demo.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.crm.entity.CAcceptanceRecord;
import org.jeecg.modules.demo.crm.entity.CProjectApplication;
import org.jeecg.modules.demo.crm.entity.CTask;
import org.jeecg.modules.demo.crm.mapper.CAcceptanceRecordMapper;
import org.jeecg.modules.demo.crm.mapper.CProjectApplicationMapper;
import org.jeecg.modules.demo.crm.service.ICAcceptanceRecordService;
import org.jeecg.modules.demo.crm.service.ICCommonService;
import org.jeecg.modules.demo.crm.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 验收记录表
 * @Author: jeecg-boot
 * @Date:   2025-06-19
 * @Version: V1.0
 */
@Service
public class CAcceptanceRecordServiceImpl extends ServiceImpl<CAcceptanceRecordMapper, CAcceptanceRecord> implements ICAcceptanceRecordService {

    @Autowired
    CAcceptanceRecordMapper cAcceptanceRecordMapper;


    @Autowired
    CProjectApplicationMapper cProjectApplicationMapper;

    @Autowired
    private ICCommonService commonService;
    @Override
    @Transactional
    public void edit(CAcceptanceRecord cAcceptanceRecord) {
        CAcceptanceRecord cAcceptanceRecord1 = cAcceptanceRecordMapper.selectById(cAcceptanceRecord.getId());
        cAcceptanceRecord1.setProjectId(cAcceptanceRecord.getProjectId());
        cAcceptanceRecord1.setSubProjectId(cAcceptanceRecord.getSubProjectId());
        cAcceptanceRecord1.setTitle(cAcceptanceRecord.getTitle());
        cAcceptanceRecord1.setAcceptanceTime(cAcceptanceRecord.getAcceptanceTime());
        cAcceptanceRecord1.setSafeguardDay(cAcceptanceRecord.getSafeguardDay());
        cAcceptanceRecord1.setFileRecordId(cAcceptanceRecord.getFileRecordId());
        cAcceptanceRecord1.setRemark(cAcceptanceRecord.getRemark());
        cAcceptanceRecordMapper.updateById(cAcceptanceRecord1);


    }

    @Override
    public void appEdit(CAcceptanceRecord cAcceptanceRecord) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        cAcceptanceRecord.setAcceptanceStatus("审核中");
        cAcceptanceRecord.setAcceptancePerson(sysUser.getId());
        cAcceptanceRecord.setReviewerUserId(null);
        cAcceptanceRecord.setReviewTime(null);
        cAcceptanceRecord.setReviewDescription("");
        cAcceptanceRecord.setLog("");
        cAcceptanceRecordMapper.updateById(cAcceptanceRecord);

    }

    @Override
    @Transactional
    public void audit(CAcceptanceRecord cAcceptanceRecord) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        CAcceptanceRecord cAcceptanceRecord1 = cAcceptanceRecordMapper.selectById(cAcceptanceRecord.getId());

        String acceptanceStatus = cAcceptanceRecord.getAcceptanceStatus();
        if("通过".equals(acceptanceStatus)){
            acceptanceStatus = "通过";
            cAcceptanceRecord1.setAcceptanceStatus(acceptanceStatus);
            cAcceptanceRecord1.setReviewerUserId(sysUser.getId());
            cAcceptanceRecord1.setReviewTime(new Date());
            cAcceptanceRecord1.setReviewDescription(cAcceptanceRecord.getReviewDescription());
            cAcceptanceRecordMapper.updateById(cAcceptanceRecord1);

/*            if(StringUtils.isNotEmpty(cAcceptanceRecord.getProjectId()) && StringUtils.isEmpty(cAcceptanceRecord.getSubProjectId())){
                //主项目验收合格
                // 同步数据到项目
                CProjectApplication cProjectApplication = cProjectApplicationMapper.selectById(cAcceptanceRecord1.getProjectId());

                if(cProjectApplication != null && "等待验收".equals(cProjectApplication.getProjectStatus())){
                    cProjectApplication.setProjectStatus("维保");
                    cProjectApplication.setAcceptanceTime(cAcceptanceRecord.getAcceptanceTime());

                    Date date = DateUtils.addDaysToDate(cAcceptanceRecord.getAcceptanceTime(),
                            cAcceptanceRecord.getSafeguardDay());
                    cProjectApplication.setMaintenanceTime(date);
                    cProjectApplicationMapper.updateById(cProjectApplication);

                }else if(cProjectApplication != null) {
                    throw new RuntimeException("项目状态不允许验收:"+cProjectApplication.getProjectStatus());
                }else {
                    throw new RuntimeException("项目不存在:");
                }
            }*/

        }else if("驳回".equals(acceptanceStatus)){
            cAcceptanceRecord1.setAcceptanceStatus(acceptanceStatus);
            cAcceptanceRecord1.setReviewerUserId(sysUser.getId());
            cAcceptanceRecord1.setReviewTime(new Date());
            cAcceptanceRecord1.setReviewDescription(cAcceptanceRecord.getReviewDescription());
            cAcceptanceRecordMapper.updateById(cAcceptanceRecord1);
        }

    }

    @Override
    public Map<String, Long> statusCount(CAcceptanceRecord acceptanceRecord, HttpServletRequest req) {
        CAcceptanceRecord query = new CAcceptanceRecord();
        Map<String, Long> data = new HashMap<>();
        List<String> statusList = new ArrayList<>();
        statusList.add("审核中");
        statusList.add("驳回");
        statusList.add("通过");
        statusList.add("全部");
        QueryWrapper<CAcceptanceRecord> queryWrapper = null;
        Boolean permissionCheck = commonService.permissionCheck();
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        for (String status : statusList) {
            queryWrapper = QueryGenerator.initQueryWrapper(query, req.getParameterMap());
            if(!status.equals("全部")){
                queryWrapper.eq("acceptance_status", status);
            }
            if(acceptanceRecord != null && acceptanceRecord.getProjectId() != null){
                queryWrapper.eq("project_id",acceptanceRecord.getProjectId());
            }
            if(acceptanceRecord != null && acceptanceRecord.getSubProjectId()!= null){
                queryWrapper.eq("sub_project_id",acceptanceRecord.getSubProjectId());
            }
            if(permissionCheck){
                queryWrapper.eq("acceptance_person",sysUser.getId());
            }
            data.put(status,cAcceptanceRecordMapper.selectCount(queryWrapper));
        }

        return data;
    }
}
